package com.aken.imdemo;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aken.imdemo.model.ContentData;
import com.aken.imdemo.model.EmotionItem;
import com.aken.imdemo.net.MessageHandleSucImp;
import com.aken.imdemo.net.TIMConversationManager;
import com.aken.imdemo.util.SpannableMaker;
import com.aken.imdemo.view.GridViewAdapter;
import com.aken.imdemo.view.MsgViewAdapter;
import com.orhanobut.logger.Logger;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<ContentData> contentDataList = new ArrayList<>();
    private MsgViewAdapter adapter;

    EditText mMsgEt;

    LinearLayout mEmotionBtn;
    RecyclerView msgListView;
    GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initContentView();

        initEmotionView();

        addMessageListener();
    }

    private void addMessageListener() {
        TIMConversationManager.INSTANCE.addMessageListener(new MessageHandleSucImp() {
            @Override
            public void handelMessage(@NotNull SpannableStringBuilder headerBuilder, @NotNull String content) {
                ContentData contentData = new ContentData(headerBuilder, content);
                contentDataList.add(contentData);
                adapter.notifyItemInserted(contentDataList.size() - 1);
                msgListView.scrollToPosition(contentDataList.size() - 1);
            }

            @Override
            public void error(String errorStr) {
                Logger.e(errorStr);
            }

        });
    }

    private void initEmotionView() {
        mMsgEt = findViewById(R.id.ed_emoji);
        mEmotionBtn = findViewById(R.id.mIBEmotion);
        gridView = findViewById(R.id.grip_view);

        mEmotionBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager manager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
                if (manager != null)
                    manager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                int visibility = gridView.getVisibility();
                if (visibility == View.VISIBLE) {
                    gridView.setVisibility(View.GONE);
                } else {
                    gridView.setVisibility(View.VISIBLE);
                }
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                EmotionItem note = (EmotionItem) adapterView.getAdapter().getItem(i);

                int start = mMsgEt.getSelectionStart();

                Editable editable = mMsgEt.getEditableText();

                Spannable spannable = SpannableMaker.buildEmotionSpannable(MainActivity.this, note.getText(), (int) mMsgEt.getTextSize());

                editable.insert(start, spannable);
            }
        });
        gridView.setAdapter(new GridViewAdapter(this));

        Button mSendMsgBtn = findViewById(R.id.mSendMsgBtn);
        mSendMsgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String msgText = mMsgEt.getText().toString();

                if (!msgText.isEmpty()) {
                    TIMConversationManager.INSTANCE.sendMessage(msgText, MainActivity.this, new MessageHandleSucImp() {
                        @Override
                        public void error(String errorStr) {
                            Logger.e(errorStr);
                        }

                        @Override
                        public void handelMessage(@NotNull SpannableStringBuilder headerBuilder, @NotNull String content) {
                            ContentData contentData = new ContentData(headerBuilder, content);
                            contentDataList.add(contentData);
                            adapter.notifyItemInserted(contentDataList.size() - 1);
                            msgListView.scrollToPosition(contentDataList.size() - 1);
                            mMsgEt.setText("");
                        }

                    });
                }
            }
        });
        mMsgEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gridView.setVisibility(View.GONE);
                msgListView.scrollToPosition(contentDataList.size() - 1);
            }
        });
    }

    private void initContentView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        msgListView = findViewById(R.id.msgListView);
        msgListView.setLayoutManager(linearLayoutManager);
        adapter = new MsgViewAdapter(contentDataList);
        msgListView.setAdapter(adapter);

        TIMConversationManager.INSTANCE.getLiveMessageList(this, new MessageHandleSucImp() {
            @Override
            public void error(String errorStr) {
                Logger.e(errorStr);
            }

            @Override
            public void handelMessage(@NotNull SpannableStringBuilder headerBuilder, @NotNull String content) {
                ContentData contentData = new ContentData(headerBuilder, content);
                contentDataList.add(contentData);
                adapter.notifyItemInserted(contentDataList.size() - 1);
                msgListView.scrollToPosition(contentDataList.size() - 1);
            }
        });
    }
}
